package com.canaan.business.service.basic.impl;

import com.canaan.business.domain.basic.WmsBaseSupplier;
import com.canaan.business.mapper.basic.WmsBaseSupplierMapper;
import com.canaan.business.service.basic.IWmsBaseSupplierService;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.TableNoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 供应商信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-12
 */
@Service
public class WmsBaseSupplierServiceImpl implements IWmsBaseSupplierService
{
    @Resource
    private WmsBaseSupplierMapper wmsBaseSupplierMapper;

    /**
     * 查询供应商信息
     *
     * @param id 供应商信息主键
     * @return 供应商信息
     */
    @Override
    public WmsBaseSupplier selectWmsBaseSupplierById(Long id)
    {
        return wmsBaseSupplierMapper.selectWmsBaseSupplierById(id);
    }

    /**
     * 查询供应商信息列表
     *
     * @param wmsBaseSupplier 供应商信息
     * @return 供应商信息
     */
    @Override
    public List<WmsBaseSupplier> selectWmsBaseSupplierList(WmsBaseSupplier wmsBaseSupplier)
    {
        return wmsBaseSupplierMapper.selectWmsBaseSupplierList(wmsBaseSupplier);
    }

    /**
     * 新增供应商信息
     *
     * @param entity 供应商信息
     * @return 结果
     */
    @Override
    public WmsBaseSupplier insertWmsBaseSupplier(WmsBaseSupplier entity)
    {
        List<String> names = Collections.singletonList(entity.getName());
        List<WmsBaseSupplier> repeatList = this.listByNoOrName(null, names);
        if(!repeatList.isEmpty()){
            throw new ServiceException("供应商名称已存在");
        }

        entity.setNo(TableNoUtil.getNo(WmsBaseSupplier.class));
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        wmsBaseSupplierMapper.insertWmsBaseSupplier(entity);
        return entity;
    }

    /**
     * 修改供应商信息
     *
     * @param entity 供应商信息
     * @return 结果
     */
    @Override
    public int updateWmsBaseSupplier(WmsBaseSupplier entity)
    {
        List<String> names = Collections.singletonList(entity.getName());
        List<WmsBaseSupplier> repeatList = this.listByNoOrName(null, names);
        boolean isRepeat = repeatList.stream().anyMatch(e -> !entity.getId().equals(e.getId()));
        if(isRepeat){
            throw new ServiceException("供应商名称已存在");
        }

        entity.setNo(TableNoUtil.getNo(WmsBaseSupplier.class));
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return wmsBaseSupplierMapper.updateWmsBaseSupplier(entity);
    }

    /**
     * 批量删除供应商信息
     *
     * @param ids 需要删除的供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsBaseSupplierByIds(Long[] ids)
    {
        return wmsBaseSupplierMapper.deleteWmsBaseSupplierByIds(ids);
    }

    /**
     * 删除供应商信息信息
     *
     * @param id 供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsBaseSupplierById(Long id)
    {
        return wmsBaseSupplierMapper.deleteWmsBaseSupplierById(id);
    }

    /**
     * 根据编号或名称查询出列表
     * @param nos 编号列表
     * @param names 名称列表
     * @return 数据列表
     */
    @Override
    public List<WmsBaseSupplier> listByNoOrName(List<String> nos, List<String> names){
        return wmsBaseSupplierMapper.listByNoOrName(nos,names);
    }


    /**
     * 导入数据
     * @param list 导入数据列表
     * @return 错误列表
     */
    @Override
    public List<String> importExcel(List<WmsBaseSupplier> list) {

        List<String> errorList = new ArrayList<>();
        if(StringUtils.isEmpty(list)){
            return errorList;
        }

        //获取重复的数据
        List<String> names = list.stream().filter(e -> e.getName() != null).map(WmsBaseSupplier::getName).collect(Collectors.toList());
        List<WmsBaseSupplier> oldList = listByNoOrName(null, names);
        names = oldList.stream().map(WmsBaseSupplier::getName).collect(Collectors.toList());

        //行数据校验
        for (int i = 0; i < list.size(); i++) {
            WmsBaseSupplier entity = list.get(i);

            if(StringUtils.isEmpty(entity.getName())){
                errorList.add("第" + ( i + 1 ) + "行缺少供应商名称");
            }else if(names.contains(entity.getName())){
                errorList.add("第" + ( i + 1 ) + "行供应商名称已存在");
            }
        }

        if(errorList.size() > 0){
            return errorList;
        }
        //没有什么问题就保存数据
        String username = SecurityUtils.getUsername();
        Date createTime = new Date();
        for (WmsBaseSupplier insetEntity : list) {
            insetEntity.setNo(TableNoUtil.getNo(WmsBaseSupplier.class));
            insetEntity.setCreateBy(username);
            insetEntity.setCreateTime(createTime);
            wmsBaseSupplierMapper.insertWmsBaseSupplier(insetEntity);
        }

        return errorList;
    }

    @Override
    public WmsBaseSupplier selectWmsBaseSupplierByName(String name) {
        return wmsBaseSupplierMapper.selectWmsBaseSupplierByName(name);
    }


}
